//
//  FeedbackPictureCollectionViewCell.swift
//  MfyClientMedical
//
//  Created by gy on 2022/9/20.
//

import UIKit

class FeedbackPictureCollectionViewCell: UICollectionViewCell {
    
    private var indexPath = IndexPath(item: 0, section: 0)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.backImgView.layerCornerRadius(4)
        }
    }
    
    func setImg(_ name: String, indexPath: IndexPath) {
        self.indexPath = indexPath
        backImgView.image = UIImage(named: name)
        delBtn.isHidden = true
    }
    func setImg(_ img: UIImage, indexPath: IndexPath) {
        self.indexPath = indexPath
        backImgView.image = img
        delBtn.isHidden = false
    }
    
    // MARK: - UIButtonAction
    @objc func delBtnAction() {
        send(event: FeedbackVC.TransferEvent.delPicture(indexPath))
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(backImgView)
        contentView.addSubview(delBtn)
        
        backImgView.snp.makeConstraints { make in
            make.left.bottom.equalToSuperview()
            make.top.equalTo(10)
            make.right.equalTo(-10)
        }
        delBtn.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        delBtn.addTarget(self, action: #selector(delBtnAction), for: .touchUpInside)
    }
    private lazy var backImgView: UIImageView = {
        let backImgView = UIImageView()
        backImgView.contentMode = .scaleAspectFill
        return backImgView
    }()
    private lazy var delBtn: AppCustomButton = {
        let delBtn = AppCustomButton()
        delBtn.imageView?.contentMode = .scaleAspectFill
        delBtn.clipsToBounds = true
        delBtn.setImage(UIImage(named: "del_red"), for: .normal)
        delBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 18, bottom: 18, right: 0)
        delBtn.isHidden = true
        return delBtn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
